var controller = "core/control.php";

$(document).ready(function() {

	$("#bodytabs ul").idTabs();

	$(".addwkedit").editable(controller, {
		submit : "OK",
		style : "display: inline",
		callback : function(value, settings) {
			refreshAvailability();
		}
	});

	$(".addlistedit").editable(controller, {
		submit : "OK",
		style : "display: inline",
		callback : function(value, settings) {
			refreshTheList();
		}
	});

	$('ul.groupWrapper').Sortable(
		{
			accept: 'groupItem',
			helperclass: 'sortHelper',
			activeclass : 	'sortableactive',
			hoverclass : 	'sortablehover',
			handle: '.itemHeader',
			tolerance: 'pointer',
			onChange : function(ser)
			{
			},
			onStart : function()
			{
				$.iAutoscroller.start(this, document.getElementsByTagName('body'));
			},
			onStop : function()
			{
				$.iAutoscroller.stop();

				$("#wklist li").each(function (i) {
					var whichwk = $(this).find(".itemHeader").text().replace(/Week /, "");
					$(this).find(".itemHeader").empty();
					$(this).find(".itemHeader").text("Week " + (i + 1));

					$.post(controller, {
						id : "updateln_wk" + (i + 1),
						week: $(this).attr("id").replace(/wk/, ""),
						order: (i + 1)
					});
				});
			}
		}
	);


	refreshTheList();
	refreshAvailability();

}); // ready function

function refreshTheList() {
	 $.ajax({
	   type: "POST",
	   url: controller,
	   data: "id=info_list",
	   success: function(data, textStatus) {
			tmpl = '<tr><td valign="top"><div id="update_list{id}name" class="edit">{name}</div></td><td valign="top"><div id="update_list{id}priority" class="editselect">{priority}</div></td><td valign="top"><div id="update_list{id}week" class="fullselect">{weeks}</div><input id="uavals{id}week" type="hidden" value="{uavals}"/></td><td valign="top"><a href="javascript:void(0);" class="removelist" id="remove_list{id}">Delete</a></td></tr>';

			output = "";
			$("#thelist .tableinfo tbody").empty();
			for(row in data.participants)
			{
				// {id}, {name}, {priority}, {weeks}
				data.participants[row].priority = data.participants[row].priority == "1" ? "Yes" : "No";
				data.participants[row].unavailable = data.participants[row].unavailable == "" ? "No Weeks" : data.participants[row].unavailable;

				output = tmpl;
				output = output.replace(/{id}/g, data.participants[row].id);
				output = output.replace(/{name}/g, data.participants[row].name);
				output = output.replace(/{priority}/g, data.participants[row].priority);
				output = output.replace(/{weeks}/g, data.participants[row].unavailable);
				output = output.replace(/{uavals}/g, data.participants[row].uavals);
				$(output).appendTo("#thelist .tableinfo tbody");
			}

			// enable jeditable functionality
			$(".edit").editable(controller, {
				submit : "OK",
				style : "display: inline"
			});

			$(".editselect").editable(controller, {
				data   : "{'1':'Yes','0':'No', 'selected':'0'}",
				type   : "select",
				submit : "OK",
				style : "display: inline"
			});

			$('.fullselect').click(function() {
				var selvals = $(this).text().split(",");
				$(this).empty();
				$(this).after($("<select class='fullselectactive' size='5' multiple='multiple'></select>"));

				$(".fullselectactive").attr("id", $(this).attr("id"));

				selvalsid = $(this).attr("id").replace(/update_list/, "uavals");
				selvals = $("#" + selvalsid).val().split(",");
				getWeekOptions(selvals);

				$(".fullselectactive").focus();

				$('.fullselectactive').blur(function() {
					var selvals = $(this).selectedValues().join(",");
					selvals = (selvals == "" || selvals.match("No Weeks") != null) ? "No Weeks" : selvals;

					selvalsid = $(this).attr("id").replace(/update_list/, "uavals");
					$("#" + selvalsid).val(selvals);

					$.post(controller,
						{id : $(this).attr("id"), value : selvals},
						function (data) {
							$('.fullselectactive').prev().empty().text(data);
							$('.fullselectactive').remove();
					});
				});
			});

			$(".removelist").click(function () {
				 $.ajax({
				   async: "false",
				   type: "POST",
				   url: controller,
				   data: "id=" + $(this).attr("id"),
				   success: function(data, textStatus) {
						refreshTheList();
				   }
				 });
			});

			$("#listtable tr:odd").css("backgroundColor", "#efefef");
	   },
	   dataType: "json"
	 });
}

function refreshAvailability() {
	$.post(controller,
		{id: "info_wk"},
		function (data) {
			tmpl = '<li id="wk{wkid}" class="groupItem"><span class="itemHeader">Week {wkorder}</span> <span class="editdate" id="update_wk{wkid}date">{wkdate}</span> <span class="edit" id="update_wk{wkid}event">{wkevent}</span> <a href="javascript:void(0);" class="removeweek" id="remove_wk{wkid}">Delete</a></li>';

			output = "";
			$("#wklist").empty();
			for(row in data.weeks)
			{
				output = tmpl;
				output = output.replace(/{wkid}/g, data.weeks[row].id);
				output = output.replace(/{wkdate}/g, data.weeks[row].date);
				output = output.replace(/{wkevent}/g, data.weeks[row].event);
				output = output.replace(/{wkorder}/g, data.weeks[row].order);
				$(output).appendTo("#wklist");
				$("ul.groupWrapper").SortableAddItem(document.getElementById("wk" + data.weeks[row].id));
			}

			// enable jeditable functionality
			$(".edit").editable(controller, {
				submit : "OK",
				style : "display: inline"
			});

			$(".editdate").editable(controller, {
				submit : "OK",
				style : "display: inline",
				callback : function(value, settings) {
					refreshTheList();
				}
			});

			$(".removeweek").click(function () {
				 $.ajax({
				   async: "false",
				   type: "POST",
				   url: controller,
				   data: "id=" + $(this).attr("id"),
				   success: function(data, textStatus) {
						refreshTheList();
						refreshAvailability();
				   }
				 });
			});
		},
		"json"
	);
}

function getWeekOptions(selvals) {
	 $.ajax({
	   async: "false",
	   type: "POST",
	   url: controller,
	   data: "id=info_wklistopts",
	   success: function(data, textStatus) {
			$(".fullselectactive").addOption(data, false);

			for(var i=0; i<selvals.length; i++)
			{
				$(".fullselectactive").selectOptions(selvals[i]);
			}
	   },
	   dataType: "json"
	 });
}